using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Example.Business;

public partial class Delete_GridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem != null)
        {
            Beer b = (Beer)e.Row.DataItem;

            LinkButton deleteBtn = (LinkButton)e.Row.FindControl("DeleteBtn");
            HyperLink availabilityLink = (HyperLink)e.Row.FindControl("AvailabilityLink");
            HyperLink styleLink = (HyperLink)e.Row.FindControl("StyleLink");
            HyperLink companyLink = (HyperLink)e.Row.FindControl("CompanyLink");

            if (deleteBtn != null) deleteBtn.Visible = b.CanDelete();
            if (availabilityLink != null) availabilityLink.Text = b.Availability.Text;
            if (styleLink != null) styleLink.Text = b.Style.Name;
            if (companyLink != null) companyLink.Text = b.Company.Name;
        }
    }
    protected void BeerDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
    {
        e.Entities = BeerCollection.FetchAll();
    }
    protected void AvailDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
    {
        e.Entities = CachedAvailabilityCollection.FetchAll();
    }
    protected void BeerStyleDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
    {
        e.Entities = CachedBeerStyleCollection.FetchAll();
    }
    protected void CompanyDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
    {
        e.Entities = CompanyCollection.FetchAll();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int beerId = Convert.ToInt32(e.Keys["BeerId"]);
        BeerInfo info = new BeerInfo();
        info.BeerId = beerId;
        Beer b = Beer.Load(info); //No extra data-access necessary.
        b.Delete(true);
      
        //or
        //BeerList bl = BeerList.FetchAll();
        //Beer b = Beer.Fetch(beerId);
        //bl.Remove(b);
        //bl.Save();

        //or
        //Beer b = Beer.Fetch(beerId);
        //b.Delete(true);
    }
}
